package br.ufmg.ubicomp.communication;

import java.util.ArrayList;
import java.util.Collection;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import br.ufmg.ubicomp.utils.EntityProperties;

public class EventCommunicationService extends CommunicationService{

	public static final String BASE_URL = ConfigurationManager.getInstance().getData("server")+ 
		"service/process?operation=list";
	
	public Collection<EntityProperties> getListService() throws Exception {

		NodeList entities = processRequest(BASE_URL);
		
		Collection<EntityProperties> services = getListOfServices(entities);								
		
		return services;
	}

	private Collection<EntityProperties> getListOfServices(
			NodeList entities) {
		Collection<EntityProperties> services = new ArrayList<EntityProperties>();		
		
		for (int i = 0; i < entities.getLength(); i++) {
			Node entityEntry = entities.item(i);
			
			if (entityEntry instanceof Element) {
				Element entityElement = (Element) entityEntry;
				
				if (entityElement.getAttribute("kind").equals("Service")) {
					EntityProperties service = getEntityElement(entityEntry);
					services.add(service);
				}
			}
		}
		return services;
	}
}
